// let host = 'http://192.168.1.102:8081'
let serverPrefix = '/canteen-wx'
let host = 'https://www.youpin.ink'
// let host = 'http://127.0.0.1'

function onRequest (url, method, data, header) {
  data = data || {}
  header = header || { 'content-type': 'application/json' }
  const token = wx.getStorageSync('token')
  if (token) {
    header['token'] = token
  }
  const promise = new Promise((resolve, reject) => {
    wx.request({
      url: host + serverPrefix + url,
      data: data,
      header: header,
      method: method,
      success: (result) => {
        if (result.statusCode === 200 && result.data.code === 401) {
          wx.redirectTo({
            url: '/pages/login/login' ,
          })
        } else if (result.statusCode === 200 && result.data.code === 500) {
          if (result.data.msg) {
            wx.showToast({
              title: result.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
          reject(result)
        } else if (result.statusCode === 200 && result.data.code === 200) {
          if (result.data.msg) {
            wx.showToast({
              title: result.data.msg,
              icon: 'success',
              duration: 2000
            })
          }
          resolve(result.data)
        }
      },
      fail: (res) => { 
        reject(res) 
      },
      complete: (res) => {
      },
    })
  })
  return promise
}

function get (url, data, header) {
    return onRequest(url, 'GET', data, header)
}

function post (url, data, header) {
  return onRequest(url, 'POST', data, header)
}

function put (url, data, header) {
  return onRequest(url, 'PUT', data, header)
}

function del (url, data, header) {
  return onRequest(url, 'DELETE', data, header)
}

function upload (url, filePath, name, formData, header) {
  header = header || { 'content-type': 'multipart/form-data' }
  formData = formData || {}
  const token = wx.getStorageSync('token')
  if (token) {
    header['token'] = token
  }
  wx.showLoading({
    title: '正在上传...',
  })
  const promise = new Promise((resolve, reject) => {
    wx.uploadFile({
      filePath: filePath,
      name: 'file',
      url: host + url,
      formData: formData,
      header: header,
      success: (result) => {
        const res = JSON.parse(result.data)
        if (result.statusCode === 200 && result.data.code === 401) {
          wx.redirectTo({
            url: '/pages/login/login' ,
          })
        } else if (result.statusCode === 200 && res.code === 500) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
          resolve(res)
        } else if (result.statusCode === 200 && res.code === 200) {
          if (res.msg) {
            wx.showToast({
              title: res.msg,
              duration: 2000
            })
          }
          resolve(res)
        }
        
      },
      fail: (res) => { 
        reject(res) 
      },
      complete: (res) => {
        wx.hideLoading()
      }
    })
  })
  return promise
}


module.exports = {
  get, post, put, del, upload
}